<%-- 
    Document   : header
    Created on : 18/07/2011, 10:55:26 AM
    Author     : David Bullock - s4206222
--%>

<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
//force the browser to reload the page
    response.setHeader("Cache-control", "no-store");

    //Login code
    if (request.getParameter("login") != null && null == session.getAttribute("username")) {
        session.setAttribute("username", request.getParameter("login"));
        session.setAttribute("userID", request.getParameter("userID"));
        session.setAttribute("userType", request.getParameter("userType"));
        //redirect the user to their homepage
        //TODO neither of these scripts work
        //response.sendRedirect("/DbPoll/" + request.getParameter("userType").toString().toLowerCase() + "/home.html");
        //out.print("<script type=\"text/javascript\">window.location = \"/DbPoll/" + request.getParameter("userType").toString().toLowerCase() + "/home.html\"</script>");
    }
// get the name of the web page - this will get the root folder if it is the index initally
    String wholePath = request.getServletPath();
    String filename = wholePath.split("/")[wholePath.split("/").length - 1];
    String dir = wholePath.substring(0, wholePath.length() - filename.length());
    filename = filename.split("\\.")[0]; //remove the file type to avoid pain
    String fileType = ".html";

    String userType = (String) session.getAttribute("userType");
    /*This section sets the type variable to be the type selected from the 
     * dropdown box (if it exists), or loads the real tempType    */
    if (request.getParameter("tempType") != null) {
        session.setAttribute("tempType", request.getParameter("tempType"));
    }
    String tempType;
    if (session.getAttribute("tempType") == null) {
        tempType = userType;
    } else {
        tempType = (String) session.getAttribute("tempType");
    }

    /* set up the array containing all the web header. 
     * Any time you want to add a new web page to the navigation, insert it here. 
     * This will ensure that the title is correct as well*/
    LinkedHashMap<String, String> Header = new LinkedHashMap<String, String>();
    //Default page that will always be there
    Header.put("home", "Home");
    LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
    LinkedHashMap<String, String> notHeader = new LinkedHashMap<String, String>();
    notHeader.put("home", "Home");
    notHeader.put("index", "Home");
    notHeader.put("viewResults", "View Poll Results");
    String frontUrl = "";
    if (tempType == null) {//output the default page list
        header.put("index", "Home");
        header.put("login", "Login");
    } else {
        frontUrl = ("/DbPoll/" + tempType.toLowerCase()) + "/";  // adds necessary user type to front of url
        header.put("/DbPoll/" + tempType.toLowerCase() + "/home", "Home");
        if (tempType.equals("Web")) {
            notHeader.put("view", "View Poll");
            notHeader.put("answer", "Answer Question");
            notHeader.put("feedback", "We Need Your Feedback");
            //nothing but home page and logout
        } else {
            if (tempType.equals("Master")) {
                header.put(frontUrl + "partList", "Create Participant List");
                notHeader.put("viewPolls", "View Polls");
                notHeader.put("reports", "Generate Report");
                notHeader.put("reportQuestion", "Result by Question");
                notHeader.put("reportQuestionResult", "Question Results");
                notHeader.put("reportDemo", "Demographic Comparison");
                notHeader.put("reportSession", "Session Participation");
                notHeader.put("admin", "Administrate Poll");
                notHeader.put("showPoll", "Manage Poll");
                notHeader.put("viewList", "Template Details");
                notHeader.put("editPartList", "Add Participant to Template");
                notHeader.put("partListDetails", "Edit Participant Details");
                notHeader.put("customDemo", "Add Demographic to Template");
                notHeader.put("successMsg", "Success!");
                notHeader.put("viewactiveuser", "View Active Users");
                notHeader.put("createTemp", "Create Template");
                notHeader.put("location", "Poll Location");

            } else if (userType.equals("Creator")) {
                notHeader.put("index", "Home");
                notHeader.put("location", "Poll Location");
                notHeader.put("Alphanumeric", "New Alphanumeric Question");
                notHeader.put("Comparative", "New Comparative Question");
                notHeader.put("Demographic", "New Demographic Question");
                notHeader.put("Numeric", "New Numeric Question");
                notHeader.put("Ranking", "New Ranking Question");
                notHeader.put("assignToPM", "Assign To Poll Master");
                notHeader.put("newMCQ", "New Multiple Choice Question");
                notHeader.put("newSRQ", "Choose Question Type");
                notHeader.put("newPM", "Create New Poll Master");
                notHeader.put("specialMCQ", "Choose Question Type");
                notHeader.put("testQuestion", "Test Question");
                notHeader.put("viewAnswers", "View Answers");
                notHeader.put("viewQuestions", "View Questions");
                notHeader.put("success", "Success");
                notHeader.put("addSpecMCQ", "New Special MC Question");
                notHeader.put("addAnswer", "Add New Answer");
                notHeader.put("deleteQuestion", "Delete Question");


            } else if (tempType.equals("Admin")) {
                notHeader.put("assignPollCreator", "Assign Poll Creator");
                header.put(frontUrl + "pollName", "Create New Poll");
                header.put(frontUrl + "report", "Session Report");
                notHeader.put("reportLocation", "Poll Location");

            } else if (tempType.equals("SysAdmin")) {
                header.put(frontUrl + "managePA", "Manage Poll Admin");
                header.put(frontUrl + "reactivatePA", "Reactivate Poll Admin");
                notHeader.put("managePADetails", "Manage Poll Admin Details");
                notHeader.put("editPADetails", "Edit Poll Admin Details");
                notHeader.put("successMsg", "Success!");
            }
            notHeader.put("createUser", "Create Users"); // will be at the end of 
            header.put("/DbPoll/createUser", "Create Users"); // will be at the end of 
            //every user bar the web user. This page will need to be only able 
            //to create the users that the tempType can do
        }
        header.put("/DbPoll/changePassword", "Change Password");
        notHeader.put("changePassword", "Change Password");
        header.put("/DbPoll/logout", "Logout");
    }
    //these are here to increase robustness and allow the back button to be used between user swaps
    notHeader.put("view", "View Poll");
    notHeader.put("answer", "Answer Question");
    notHeader.put("feedback", "We Need Your Feedback");
    notHeader.put("changePassword", "Change Password");
    notHeader.put("createUser", "Create Users");
    notHeader.put("managePADetails", "Manage Poll Admin Details");
    notHeader.put("editPADetails", "Edit Poll Admin Details");
    notHeader.put("successMsg", "Success!");
    notHeader.put("assignPollCreator", "Assign Poll Creator");
    notHeader.put("index", "Home");
    notHeader.put("location", "Poll Location");
    notHeader.put("Alphanumeric", "New Alphanumeric Question");
    notHeader.put("Comparative", "New Comparative Question");
    notHeader.put("Demographic", "New Demographic Question");
    notHeader.put("Numeric", "New Numeric Question");
    notHeader.put("Ranking", "New Ranking Question");
    notHeader.put("assignToPM", "Assign To Poll Master");
    notHeader.put("newMCQ", "New Multiple Choice Question");
    notHeader.put("newSRQ", "Choose Question Type");
    notHeader.put("newPM", "Create New Poll Master");
    notHeader.put("specialMCQ", "Choose Question Type");
    notHeader.put("testQuestion", "Test Question");
    notHeader.put("viewAnswers", "View Answers");
    notHeader.put("viewQuestions", "View Questions");
    notHeader.put("success", "Success");
    notHeader.put("addSpecMCQ", "New Special MC Question");
    notHeader.put("addAnswer", "Add New Answer");
    notHeader.put("deleteQuestion", "Delete Question");
    notHeader.put("showPoll", "Manage Poll");
    notHeader.put("viewList", "Template Details");
    notHeader.put("editPartList", "Add Participant to Template");
    notHeader.put("partListDetails", "Edit Participant Details");
    notHeader.put("customDemo", "Add Demographic to Template");
    notHeader.put("successMsg", "Success!");
    notHeader.put("viewactiveuser", "View Active Users");
    notHeader.put("viewPolls", "View Polls");
    notHeader.put("reports", "Generate Report");
    notHeader.put("reportQuestion", "Result by Question");
    notHeader.put("reportQuestionResult", "Question Results");
    notHeader.put("reportDemo", "Demographic Comparison");
    notHeader.put("reportSession", "Session Participation");
    notHeader.put("admin", "Administrate Poll");
    notHeader.put("message", "Send Message");
    String title = (header.get(frontUrl + filename) != null)
            ? header.get(frontUrl + filename) : notHeader.get(filename);
%>

<link rel="stylesheet" type="text/css" href="/DbPoll/style.css"/>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="/DbPoll/style.css" media="screen" />
        <title>dbPoll | <% out.print(title);%></title>
    </head>
    <body>
        <div id="upper_container">
            <div id = "logo">
                <table>
                    <tbody width="780" cellspacing="0" cellpadding="0" border="0">
                        <tr>
                            <td><a valign="middle" align="left">
                                    <img width="213" height="51" border="0" alt="Keepad Logo" src="/DbPoll/images/keepad_logo.jpg"/>
                                </a></td>
                            <td width="350" align="right"><a>
                                    <img alt="NSW Government Approved Supplier" src="/DbPoll/images/nsw.jpg"/>
                                </a></td>
                        </tr>
                    </tbody>
                </table>
            </div>                               
            <div id="header_title">
                <%=title%>  
            </div>
            <div id="loginStuff">
                <%
                    if (null != session.getAttribute("username")) {
                        out.println("Welcome " + session.getAttribute("username"));
                    }
                %>
                <%
                    String[] types = {"Web", "Master", "Creator", "Admin", "SysAdmin"};
                    if (tempType != null && !userType.equals("SysAdmin")) {
                        out.print("<select onchange=\"updateType()\" id = \"select\">");
                        for (String type : types) {
                            if (type.equals(tempType)) {
                                out.print("<option selected=\"true\">" + type + "</option>");
                            } else {
                                out.print("<option>" + type + "</option>");
                            }
                            if (type.equals(userType)) {
                                break;
                            }
                        }
                    }
                    out.print("</select>");
                %>
                <script language="Javascript">
                    function updateType(){
                        var xmlhttp;
                        if (window.XMLHttpRequest)
                        {// code for IE7+, Firefox, Chrome, Opera, Safari
                            xmlhttp=new XMLHttpRequest();
                        }
                        else
                        {// code for IE6, IE5
                            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        type = document.getElementById("select").value;
                                
                        xmlhttp.open("GET","/DbPoll/index.html?tempType="+type,false);
                        xmlhttp.send();
                                
                        window.location = "/DbPoll/"+type.toString().toLowerCase()+"/home.html";
                    }
                </script>

            </div>

            <div id="navigation">
                <!-- main menu -->
                <ul>
                    <%
                        Set st = header.keySet();
                        Iterator itr = st.iterator();
                        Object key;
                        while (itr.hasNext()) {
                            key = itr.next();
                            if (!notHeader.containsKey(key)) {
                                out.print("<li><a href=\"" + key + fileType + "\">" + header.get(key) + "</a></li>");
                            }
                        }%>     
                </ul>
            </div>

        </div>

        <div id="main_container">
